/*
 * Copyright (c) 2009 Frederic Daoud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.stripesbook.rayures.view.util;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import net.sourceforge.stripes.util.HtmlUtil;

/**
 * Contains utility methods for formatting purposes. An instance of this object is added to the
 * context under the {@value org.stripesbook.rayures.util.Constants#ATTR_FORMATTER} key.
 *
 * @author Frederic Daoud
 */
public class Formatter {
  private Locale locale;

  /**
   * Constructs a formatter object, using the given locale as the formatter's locale.
   */
  public Formatter(Locale locale) {
    if (locale == null) {
      throw new NullPointerException("locale is null");
    }
    this.locale = locale;
  }

  /**
   * Formats the given number as a currency for the formatter's locale.
   */
  public String currency(Number number) {
    return NumberFormat.getCurrencyInstance(locale).format(number.doubleValue());
  }

  /**
   * Formats the given date, using the default date format for the formatter's locale.
   */
  public String date(Date date) {
    if (date == null) {
      throw new NullPointerException("date is null");
    }
    return DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(date);
  }

  /**
   * Formats the given date, using the given pattern and the formatter's locale.
   */
  public String date(Date date, String pattern) {
    return new SimpleDateFormat(pattern, locale).format(date);
  }

  /**
   * Formats the given decimal value, using the given pattern and the formatter's locale.
   */
  public String decimal(Number number, String pattern) {
    return new DecimalFormat(pattern).format(number.doubleValue());
  }

  /**
   * Uses {@link HtmlUtil#encode(String)} to output escaped HTML code. Use this method to sanitize
   * strings before outputting them in a template, preventing scripting attacks.
   */
  public String html(String string) {
    return HtmlUtil.encode(string);
  }

  /**
   * Formats the message using {@link MessageFormat}, passing any arguments to the formatter.
   */
  public String message(String message, Object... arguments) {
    return MessageFormat.format(message, arguments);
  }

  /**
   * Formats the given value as a percentage, using the given pattern and the formatter's locale.
   */
  public String percentage(Number number) {
    return NumberFormat.getPercentInstance(locale).format(number.doubleValue());
  }

  /**
   * Formats the current date and time, using the default format and the formatter's locale.
   */
  public String time() {
    return time(new Date());
  }

  /**
   * Formats the given date, including the time, using the default format and the formatter's locale.
   */
  public String time(Date date) {
    return DateFormat.getTimeInstance(DateFormat.DEFAULT).format(date);
  }

  /**
   * Formats the current date, using the default format and the formatter's locale.
   */
  public String today() {
    return DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(new Date());
  }

  /**
   * Formats the current date, using the given pattern and the formatter's locale.
   */
  public String today(String pattern) {
    return new SimpleDateFormat(pattern, locale).format(new Date());
  }
}
